package com.sky.controller.user;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController(value = "userDishController")
@Slf4j
@RequestMapping("/user/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
//    @GetMapping("/list")
//    public Result<List<DishVO>> list(Long categoryId){
//        log.info("根据分类id查询菜品:{}",categoryId);
////       Dish dish=new Dish();
////        dish.builder().
////                categoryId(categoryId).
////                status(StatusConstant.ENABLE).
////                build();
//        List<DishVO> list=dishService.GetDishByCategoryId(categoryId);
//        return Result.success(list);
//
//    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result<List<DishVO>> list1(Long categoryId){
        log.info("根据分类id查询菜品:{}",categoryId);
        String key="dish_"+categoryId;
        //判断缓存中是否存在数据
        List<DishVO> list= (List<DishVO>) redisTemplate.opsForValue().get(key);
        if(list!=null &&list.size()>0){
            return Result.success(list);
        }
        //如果缓存中没有数据，则查询数据库
        Dish dish=new Dish();
        dish=dish.builder().
                categoryId(categoryId).
                status(StatusConstant.ENABLE).
                build();
         list=dishService.GetDishByCategoryId1(dish);
         //将数据存入缓存
        redisTemplate.opsForValue().set(key,list);
        return Result.success(list);

    }



}
